/**
 * Created by joustany on 17/12/7.
 */
var params = JSON.parse(window.localStorage.user);
console.log(params)
var redirect = JSON.parse(decodeURIComponent(params['redirect']));
var order_no = $.urlGet().order_no
console.log(order_no)
$.ajax({
    url: configApi('/order/view'),
    data: {
        no:order_no,
        access_token:redirect.token,
    },
    dataType: 'json',
    type: "get",
    timeout: 8000 //超时时间设置，单位毫秒
}).done(function (res) {
    if (res.success){
        var data = res.data
        orderList(data)
    }else {
        layer.open({
            content: res.message
            ,skin: 'msg'
            ,time: 2 //2秒后自动关闭
        });
    }
}).fail(function () {
    layer.open({
        content: "服务器异常,请退出重试"
        ,skin: 'msg'
        ,time: 2 //2秒后自动关闭
    });
})
function orderList(foods) {
    "use strict"
    console.log(foods.shareButton)
    var html = ''
    $.each(foods.orderItem,function (index,item) {
        html +='<div class="food-content">'
        html +='<div class="food">'
        html +='<div class="name"><img src="'+ item.item.img +'" alt="">'+ item.name +'</div>'
        html +='<div class="count"><span class="num">x&nbsp<span>'+ item.num +'</span></span><span class="food-price">¥<span class="price">'+ item.price +'</span></span></div>'
        html +='</div>'
        html +='</div>'
    })
    $('.gods-list').html(html)
    if (foods.coupon_price > 0){
        $('.discounts').show()
        $('.discounts—price .price').text(foods.coupon_price)
    }else {
        $('.discounts').hide()
    }
    if (foods.discount_amount == 0){
        $('.discount').hide()
    }else {
        $('.discount—price .price').text(foods.discount_amount)
    }
    $('.price-container .num').text(foods.htotal)
    if (foods.shareButton == '0'){
        $('.share_icon').hide()
    }else {
        if (params.platform == 'dingTalk'){
            $('.share_icon').hide()
        }else {
            $('.share_icon').show()
        }
        if(params.platform == 'wechat'){
            //微信
            wechat()
            $('.share_icon').click(function () {
                "use strict"
                $('.share-container').show()
            })

        }else {
            ready(function () {
                aliShare()
                document.querySelector('.share_icon').addEventListener('click', function () {
                    Alipay()
                })
            })
        }
    }
}
$('.submit').click(function () {
    window.location.href = './index.html'
})
// 微信调用
function wechat() {
    $.ajax({
        url: configApi('/coupon/share-coupon'),
        data: {
            no:order_no,
            access_token:redirect.token
        },
        dataType: 'json',
        type: "post",
    }).done(function (res) {
        console.log(res)
        if (res.success){
            console.log(res)
            var data = res.data
            var title = data.text.title,
                desc = data.text.desc,
                img = data.text.icon,
                shareId = data.link,
                link = window.location.origin + '/webApp/shareParams.html?lineLink='+ shareId +''
            console.log(link)
            console.log(shareId)
            wxShare(title,desc,img,link,redirect.token,shareId.split('share_id=')[1])
        }else {
            layer.open({
                content: res.message
                ,skin: 'msg'
                ,time: 2 //2秒后自动关闭
            });
        }
    }).fail(function () {
        layer.open({
            content: "服务器异常,请退出重试"
            ,skin: 'msg'
            ,time: 2 //2秒后自动关闭
        });
    })
    $('.share_content').hide()
}
//支付宝调用
function ready(callback) {
    // 如果jsbridge已经注入则直接调用
    if (window.AlipayJSBridge) {
        callback && callback();
    } else {
        // 如果没有注入则监听注入的事件
        document.addEventListener('AlipayJSBridgeReady', callback, false);
    }
}
function Alipay() {
    $.ajax({
        url: configApi('/coupon/share-coupon'),
        data: {
            no:order_no,
            access_token:redirect.token
        },
        dataType: 'json',
        type: "post",
    }).done(function (res) {
        console.log(res)
        if (res.success){
            console.log(res.data)
            var data = res.data
            var title = data.text.title,
                desc = data.text.desc,
                img = data.text.icon,
                shareId = data.link,
                link = window.location.origin + '/webApp/shareParams.html?lineLink='+ shareId +''
            AlipayJSBridge.call("startShare", {
                'onlySelectChannel': [
                    "Weibo",
                    "ALPContact",
                    "ALPTimeLine",
                    "SMS",
                    "Weixin",
                    "WeixinTimeLine",
                    "QQ",
                    "QQZone",
                    "OpenInSafari",
                ]
            },function (data) {
                AlipayJSBridge.call("shareToChannel", {
                    name: data.channelName,
                    param: {
                        title: title,
                        content: desc,
                        imageUrl: img,
                        captureScreen: false, //是否分享当前页面的截图
                        url: link,
                    }
                },function (result) {
                    // layer.open({
                    //     content:'分享成功'
                    //     ,skin: 'msg'
                    //     ,time: 2 //2秒后自动关闭
                    // });
                    shareHaer(redirect.token,shareId.split('share_id=')[1])
                })
            })
        }else {
            layer.open({
                content: res.message
                ,skin: 'msg'
                ,time: 2 //2秒后自动关闭
            });
        }
    }).fail(function () {
        layer.open({
            content: "服务器异常,请退出重试"
            ,skin: 'msg'
            ,time: 2 //2秒后自动关闭
        });
    })
}
$('.share-container').click(function () {
    $('.share-container').hide()
})
